﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyCalManager.Helper
{
    public class InfoTextBox : TextBox
    {
        

        // Fields
        //private RoutedEventHandler handler = null;
        
        private static readonly DependencyPropertyKey HasTextPropertyKey = DependencyProperty.RegisterReadOnly("HasText", typeof(bool), typeof(MyCalManager.Helper.InfoTextBox), new FrameworkPropertyMetadata(false));
        public static readonly DependencyProperty TextBoxInfoProperty = DependencyProperty.Register("TextBoxInfo", typeof(string), typeof(MyCalManager.Helper.InfoTextBox), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty HasTextProperty = HasTextPropertyKey.DependencyProperty;


        public static readonly DependencyProperty InputTypeProperty = DependencyProperty.Register("InputType", typeof(InputTypes), typeof(MyCalManager.Helper.InfoTextBox), new PropertyMetadata(InputTypes.Text));
        public static readonly DependencyProperty IsValidProperty = DependencyProperty.Register("IsValid", typeof(bool), typeof(MyCalManager.Helper.InfoTextBox), new PropertyMetadata(false));
        
       
        static InfoTextBox()
        {
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCalManager.Helper.InfoTextBox), new FrameworkPropertyMetadata(typeof(MyCalManager.Helper.InfoTextBox)));
            TextBox.TextProperty.OverrideMetadata(typeof(MyCalManager.Helper.InfoTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(MyCalManager.Helper.InfoTextBox.TextPropertyChanged)));
        }

        public InfoTextBox()
        {
            this.Loaded += (s, e) => isValid();
        }



        private static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            MyCalManager.Helper.InfoTextBox box = (MyCalManager.Helper.InfoTextBox)sender;

            box.isValid();
            bool flag = box.Text.Length > 0;
            if (flag != box.HasText)
            {
                box.SetValue(HasTextPropertyKey, flag);
            }
        }


        private void isValid()
        {
            switch (this.InputType)
            {
                case InputTypes.Int:
                    int a;
                    if (int.TryParse(Text, out a))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                        SetTextBoxProperties(false);
                    break;
                case InputTypes.Decimal:
                    decimal b;
                    if (decimal.TryParse(Text, out b))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                        SetTextBoxProperties(false);
                    break;
                case InputTypes.Float:
                    float c;
                    if (float.TryParse(Text, out c))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                        SetTextBoxProperties(false);
                    break;
                case InputTypes.Text:
                    SetTextBoxProperties(true);
                    break;
                case InputTypes.IntNotNull:
                    int d;
                    if (int.TryParse(Text, out d) && !string.IsNullOrEmpty(Text))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                        SetTextBoxProperties(false);
                    break;
                case InputTypes.DecimalNotNull:
                    decimal result;
                    if (decimal.TryParse(Text, out result) && !string.IsNullOrEmpty(Text))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                    {
                        SetTextBoxProperties(false);
                    }
                    break;
                case InputTypes.TextNotNull:
                    if (!string.IsNullOrEmpty(Text))
                    {
                        SetTextBoxProperties(true);
                    }
                    else
                        SetTextBoxProperties(false);
                    break;
            }
            
        }

        private void SetTextBoxProperties(bool valid)
        {
            this.IsValid = valid;
            if (valid)
            {
                base.BorderBrush = (Brush)TryFindResource("DefaultBorderBrush");
                base.ToolTip = null;
            }
            else
            {
                base.BorderBrush = (Brush)TryFindResource("FaultyBorderBrush");
                base.ToolTip = "Input not Valid !";
            }
        }

        // Properties

        public InputTypes InputType
        {
            get
            {
                return (InputTypes)base.GetValue(InputTypeProperty);
            }
            set
            {
                base.SetValue(InputTypeProperty, value);
            }
        }

        public bool IsValid 
        {
            get
            {
                return (bool)base.GetValue(IsValidProperty);
            }
            set
            {
                base.SetValue(IsValidProperty, value);
            }
        }


        public bool HasText
        {
            get
            {
                return (bool)base.GetValue(HasTextProperty);
            }
        }

        public string TextBoxInfo
        {
            get
            {
                return (string)base.GetValue(TextBoxInfoProperty);
            }
            set
            {
                base.SetValue(TextBoxInfoProperty, value);
            }
        }

       

    }


}
